package com.note.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.note.app.dto.CommentCreateDTO;
import com.note.app.dto.CommentDTO;
import com.note.app.service.CommentService;
import com.note.common.context.UserContext;
import com.note.common.vo.ResponseVo;
import com.note.app.annotation.LogRecord;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 评论控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/comments")
@RequiredArgsConstructor
@Tag(name = "评论管理", description = "评论相关接口")
public class CommentController {

    private final CommentService commentService;
    
    @PostMapping
    @Operation(summary = "创建评论", description = "创建笔记评论")
    @LogRecord(description = "添加笔记评论", module = "评论管理", operationType = LogRecord.OperationType.CREATE)
    public ResponseVo<Long> createComment(@Valid @RequestBody CommentCreateDTO dto) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(commentService.createComment(userId, dto));
    }
    
    @DeleteMapping("/{commentId}")
    @Operation(summary = "删除评论", description = "删除笔记评论")
    @LogRecord(description = "删除笔记评论", module = "评论管理", operationType = LogRecord.OperationType.DELETE)
    public ResponseVo<Boolean> deleteComment(
            @Parameter(description = "评论ID") @PathVariable Long commentId) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(commentService.deleteComment(userId, commentId));
    }
    
    @GetMapping("/note/{noteId}")
    @Operation(summary = "获取笔记评论", description = "分页获取笔记的评论列表")
    public ResponseVo<Page<CommentDTO>> getNoteComments(
            @Parameter(description = "笔记ID") @PathVariable Long noteId,
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") long size) {
        return ResponseVo.success(commentService.getNoteComments(noteId, current, size));
    }
    
    @GetMapping("/user")
    @Operation(summary = "获取用户评论", description = "分页获取当前用户的评论列表")
    public ResponseVo<Page<CommentDTO>> getUserComments(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") long current,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "10") long size) {
        Long userId = UserContext.getUserId();
        return ResponseVo.success(commentService.getUserComments(userId, current, size));
    }
    
    @GetMapping("/count/{noteId}")
    @Operation(summary = "获取笔记评论数", description = "获取指定笔记的评论数量")
    public ResponseVo<Integer> getCommentCount(
            @Parameter(description = "笔记ID") @PathVariable Long noteId) {
        return ResponseVo.success(commentService.countCommentsByNoteId(noteId));
    }
} 